<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>库存查询</title>
    <script type="text/javascript">
      Ext.onReady(function(){

        var productTemplate = new Ext.XTemplate('<tpl for="."><div  class="search-item"><div style="float:left; width:230px">{name}</div><div style="float:left; width:80px">{number}</div><div>', '{is_suit}', '</div></div></tpl>');
        var productStore = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: "/product/getAllProducts?for_cbb=true"
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'number'
            }, {
              name: 'name'
            }, {
              name: 'spec'
            }, {
              name: 'unit'
            }, {
              name: 'unit_price'
            }, {
              name: 'is_suit',
              convert:function(value,record){
                if(value == "1"){
                  return "套装";
                }else if(value == "0"){
                  return "单品";
                }
              }
            }])
        });
        //所有产品
        var productCBB = new Ext.form.ComboBox({
          store: productStore,
          fieldLabel: '产品名称',
          emptyText: '请选择...',
          //typeAhead: false,
          loadingText: '搜索中...',
          anchor: '95%',
          pageSize: 25,
          //hideTrigger: false,
          tpl: productTemplate,
          minChars: 0,
          itemSelector: 'div.search-item',
          valueField: 'id',
          displayField: 'name',
          hiddenName: 'product_id',//如果不想提交displayField，则在这儿指定要提交的Key，value就是valueField．
          //triggerAction:"all",
          selectOnFocus:true
        });
                
        //所有仓库
        var warehouseTemplate = new Ext.XTemplate('<tpl for="."><div class="search-item">', '{number}|{name}', '</div></tpl>');
        var warehouseStore = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: pageSize + 10
          },
          proxy: new Ext.data.HttpProxy({
            url: "/warehouse/getAllWarehouse?for_cbb=true"
          }),
          reader: new Ext.data.JsonReader({
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'number'
            }, {
              name: 'name'
            }])
        });

        var warehouse = new Ext.form.ComboBox({
          store: warehouseStore,
          fieldLabel: '仓库',
          emptyText: '请选择...',
          valueField: 'id',
          displayField: 'name',
          hiddenName: 'warehouse_id',
          //typeAhead: false,
          loadingText: '搜索中...',
          anchor: '95%',
          //pageSize: 10,
          //hideTrigger: false,
          tpl: warehouseTemplate,
          minChars: 0,
          itemSelector: 'div.search-item',
          triggerAction:'all',
          editable:false,
          selectOnFocus:true
        });

        var showBatchInfo = new Ext.form.Checkbox({
          name: 'batch_info',
          clearCls: 'allow-float',
          itemCls: 'float-left',
          fieldLabel: '显示批次'
        })

        var actionDate = new Ext.form.DateField({
          fieldLabel: '截止日期',
          name: 'action_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",
          inputType: '2011-01-01',
          anchor: '100%'
        });
        
        var searchButton = new Ext.Button({
          text: '搜索',
          iconCls: 'finding',
          handler: function(){
            stockDS.removeAll();
            stockDS.baseParams.warehouse_id = warehouse.getValue();
            stockDS.baseParams.product_id = productCBB.getValue();
            stockDS.baseParams.batch_info = showBatchInfo.getValue();
            stockDS.baseParams.action_date = actionDate.getRawValue();
            //stockDS.baseParams.begin_date = beginDate.getRawValue();
            //stockDS.baseParams.end_date = endDate.getRawValue();      
            stockDS.load({callback: function(record, options, success){
                if (financialManagement === '是'){
                  costAmountLabel.setText("成本总值:"+Ext.util.Format.number(stockDS.reader.jsonData.sum_cost_amount,'0,000.00'));
                  taxAmountLabel.setText("成本总值(含税):"+Ext.util.Format.number(stockDS.reader.jsonData.sum_tax_amount,'0,000.00'));
                }
              }
            });
          }
        });

        var clearButton = new Ext.Button({
          text: '清空',
          width:50,
          handler: function(){
            queryFormPanel.form.reset();
            //orderNumber.setValue('');
          }
        });

        var exportXLSButton = new Ext.Button({
          text: '导出为Excel文件',
          iconCls: 'xls',
          handler: function(){
            if (queryFormPanel.form.isValid()) {
              var queryStr = "/stock/productQuery?sub_type=excel&";
              queryStr += "warehouse_id=" + warehouse.getValue();
              queryStr += "&product_id=" + productCBB.getValue();
              queryStr += "&batch_info=" + showBatchInfo.getValue();
              queryStr += "&action_date=" + actionDate.getRawValue();
              window.open(queryStr,'_blank','width=1,height=1,toolbar=no,menubar=no,location=no');
            }
          }
        });
        
        var queryFormPanel = new Ext.form.FormPanel({
          //baseCls: 'x-plain',
          //renderTo: 'query_form',
          labelWidth: 55,
          width: screenWidth-200,
          labelAlign: 'right',
          frame : true,
          border:false,
          items: [{
              xtype: 'fieldset',
              defaults: {width: 1180},
              title: '查询条件',
              border: true,
              items: [{
                  layout: 'column',
                  border: false,
                  items: [{
                      columnWidth: .15,
                      layout: 'form',
                      border: false,
                      labelWidth:30,
                      items: [warehouse]
                    }, {
                      columnWidth: .4,
                      layout: 'form',
                      border: false,
                      items: [productCBB]
                    }, {
                      columnWidth: .07,
                      layout: 'form',
                      border: false,
                      items: [showBatchInfo]
                    }, {
                      columnWidth: .14,
                      layout: 'form',
                      border: false,
                      items: [actionDate]
                    }, {
                      columnWidth: .05,
                      layout: 'form',
                      border: false,
                      items: [searchButton]
                    }, {
                      columnWidth: .05,
                      layout: 'form',
                      border: false,
                      items: [clearButton]
                    }, {
                      columnWidth: .1,
                      layout: 'form',
                      border: false,
                      items: [exportXLSButton]
                    }]
                }]
            }]
        });

        //成本总值
        var costAmountLabel = new Ext.form.Label({
          name: 'cost_amount_label'
        })
        costAmountLabel.setText("成本总值:0");

        //成本总值(含税)
        var taxAmountLabel = new Ext.form.Label({
          name: 'tax_amount_label'
        })
        costAmountLabel.setText("成本总值:0");
        taxAmountLabel.setText("成本总值(含税):0");
        //查询结果列表
        var stockDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: pageSize + 10
          },
          proxy: new Ext.data.HttpProxy({
            url: '/stock/productQuery'
          }),
          remoteSort:true,
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name:'order_number',
              mapping:"resource",
              convert:function(value,record){
                if(value == ""){
                  return "";
                }else{
                  return value.order_number;
                }
              }
            },{
              name: 'product_number',
              mapping:'product.number'
            },{
              name: 'product_name',
              mapping:'product.name'
            },{
              name: 'quantity',
              type: 'int'
            },{
              name: 'projected_quantity',
              type: 'int'
            },{
              name: 'warehouse_name',
              mapping:'warehouse.name'
            },{
              name: 'description'
            },{
              name: 'product_cost_price',
              mapping:'product.cost_price'
            },{
              name: 'action_date'
            },{
              name: 'created_at'
            }])
        });
        var stockSM = new Ext.grid.CheckboxSelectionModel();
        var stockCM = new Ext.grid.ColumnModel({defaults: {
            sortable: true,
            menuDisabled: false
          },columns:[new Ext.grid.RowNumberer(),stockSM,{
              header: 'id',
              dataIndex: 'id',
              width:50
            }, {
              header: '单号',
              dataIndex: 'order_number',
              width:130
            }, {
              header: '产品名称',
              dataIndex: 'product_name',
              width:240
            }, {
              header: '产品编号',
              dataIndex: 'product_number',
              width:100
            }, {
              header: '实际数量',
              align: 'right',
              dataIndex: 'quantity',
              width:75
            }, {
              header: '预计数量',
              align: 'right',
              dataIndex: 'projected_quantity',
              width:75
            }, {
              header: '仓库名称',
              dataIndex: 'warehouse_name'
            }, {
              header: '成本均价',
              align: 'right',
              renderer: moneyPrivilegeRender,
              dataIndex: 'product_cost_price',
              width:75
            }, {
              header: '描述',
              dataIndex: 'description',
              width:150
            }, {
              header: '仓库操作日期',
              dataIndex: 'action_date',
              width:90,
              renderer: Ext.util.Format.dateRenderer('Y-m-d')
            }, {
              header: '生成日期',
              dataIndex: 'created_at',
              width:110,
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i')
            }]});
        
        var stockGrid =  new Ext.grid.GridPanel({
          width: screenWidth-170,
          height: screenHeight-360,
          loadMask: true,
          ds: stockDS,
          cm: stockCM,
          sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
          //enableHdMenu: false,
          stripeRows:true,
          iconCls: 'icon-grid',
          //collapsible: true,
          //animCollapse: false,
          title:'查询结果',
          frame: true,
          bbar: [new Ext.PagingToolbar({
              pageSize: pageSize + 10,
              store: stockDS,
              displayInfo: true,
              displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
              emptyMsg: "没有记录"
            }),'->',taxAmountLabel,'-',costAmountLabel]
        });
        

        //总的panel
        var workbenchPanel = new Ext.Panel({
          width: screenWidth-170,
          height: screenHeight-270,
          frame:false,
          layout:'table',
          layoutConfig: {columns:1},
          defaults:{margins:'5 5 5 5'},
          //bodyStyle: 'padding:15px;align:center',
          //listeners: {
          //expand: showAgileDiagram
          //},
          items:[queryFormPanel,stockGrid]
        });
        workbenchPanel.render("query_workbanch");
        
      })
    </script>
  </head>
  <body>
    <div id="query_workbanch"></div>
  </body>
</html>
